package com.kady.controller.order;

import com.kady.contant.BeanFactoryConstant;
import com.kady.contant.MessageConstant;
import com.kady.controller.BaseServlet;
import com.kady.entity.Order;
import com.kady.entity.OrderDeatil;
import com.kady.entity.ResultVO;
import com.kady.factory.BeanFactory;
import com.kady.service.OrderService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/backend/order")
public class OrderController extends BaseServlet {

    private OrderService orderService = (OrderService)BeanFactory.getBean(BeanFactoryConstant.ORDER_SERVICE);

    /**
     * 获取订单列表
     */
    public String list (HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException, InvocationTargetException, IllegalAccessException {
        List<Order> list= orderService.getOrderList();

        req.setAttribute("orders",list);
        return MessageConstant.PREFIX_FORWARD + "/backend/detail/order/order-list.jsp";
    }

    /**
     * 订单结算
     */
    public Object settle (HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException, InvocationTargetException, IllegalAccessException {
        String orderId = req.getParameter("orderId");
        Object res = orderService.settleOrder(orderId);
        if(res!=null && res instanceof ResultVO){
            return res;
        }
        return MessageConstant.PREFIX_FORWARD + "/backend/order?method=list";
    }

    /**
     * 获取订单详情
     */
    public Object detail (HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException, InvocationTargetException, IllegalAccessException {
        String orderId = req.getParameter("orderId");
        String userId = req.getParameter("userId");
        List<OrderDeatil> list = orderService.getDetailList(orderId,userId);
        req.setAttribute("details",list);
        return MessageConstant.PREFIX_FORWARD + "/backend/detail/order/order-detail.jsp";
    }



}
